package com.example;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        
        // this.getInitParameter(name);  //获取初始化参数
        // this.getServletConfig();            // servlet 配置
        // this.getServletContext();           // servlet 上下文

        resp.setContentType("text/html");
        // 获取url中的参数
        String name = req.getParameter("name");
        if (name == null) {
            name = "world!";
        }

        PrintWriter pw = resp.getWriter();
        pw.write("<h1>Hello," + name + "!<h1>");
        pw.write("<p><a href=\"/setcontext?name=" + name + "\">setcontext</a></p>");
        pw.flush();

        System.out.println(req.getLocalAddr() + " " + req.getMethod());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
